package com.yuke.cloud.service.smc.job;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuke.cloud.common.util.DateUtil;
import com.yuke.cloud.service.api.omc.dto.OdsOrderApiDto;
import com.yuke.cloud.service.api.omc.service.OmcOrderFeignApi;
import com.yuke.cloud.service.smc.dto.DmsStoreDto;
import com.yuke.cloud.service.smc.entity.DmsStore;
import com.yuke.cloud.service.smc.service.DmsStoreService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: 金天
 */
@Component
@Slf4j
@EnableScheduling
public class StoreQuartz {

    @Resource
    private OmcOrderFeignApi omcOrderFeignApi;

    @Resource
    private DmsStoreService dmsStoreService;

    /**
     * 每月一号
     */
    @Scheduled(cron = "0 0 1 1 * ?")
    public void scheduledMethod(){

        //将shopId分为三组,评级为3,4,5
        List<Long> list3 = new ArrayList<>();
        List<Long> list4 = new ArrayList<>();
        List<Long> list5 = new ArrayList<>();

        Date begin = DateUtil.lastMonthTimeBegin();
        Date end = DateUtil.lastMonthTimeEnd();

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        String beginTime = formatter.format(begin);

        String endTime = formatter.format(end);


        Example example = new Example(DmsStore.class);

        Example.Criteria criteria = example.createCriteria();

        criteria.andIsNotNull("shopId").andNotEqualTo("shopId",0);

        int num = dmsStoreService.selectCountByExample(example);

        //一页五百条
        int size = 500;

        for (int page = 1; page <= ((num - 1) / size + 1);page++) {

            List<DmsStoreDto> shopIdStore = dmsStoreService.findShopIdStore(page,size);

            List<Long> shopIdList = shopIdStore.stream().map(DmsStoreDto::getShopId).collect(Collectors.toList());

            //上个月的总金额
            List<OdsOrderApiDto> odsOrderApiDtos = omcOrderFeignApi.countByShopIds(shopIdList,beginTime,endTime);


            if (odsOrderApiDtos!=null&&odsOrderApiDtos.size()!=0) {

                List<Long> partShopIdList = odsOrderApiDtos.stream().map(OdsOrderApiDto::getShopId).collect(Collectors.toList());

                //没下过单的shopId作为等级5
                shopIdList.removeAll(partShopIdList);

                list5.addAll(shopIdList);
            }
            //根据金额判断等级
            if(odsOrderApiDtos != null && !odsOrderApiDtos.isEmpty()) {
                for (OdsOrderApiDto odsOrderApiDto : odsOrderApiDtos) {

                    if (odsOrderApiDto.getAmountPayable().longValue() >= 1000000) {
                        list3.add(odsOrderApiDto.getShopId());
                        continue;
                    }
                    if (odsOrderApiDto.getAmountPayable().longValue() >=20000&&odsOrderApiDto.getAmountPayable().longValue() <1000000) {
                        list4.add(odsOrderApiDto.getShopId());
                        continue;
                    }
                    if (odsOrderApiDto.getAmountPayable().longValue() <20000) {
                        list5.add(odsOrderApiDto.getShopId());
                    }
                }
            }
        }
        dmsStoreService.batchUpdateList(3,list3);
        dmsStoreService.batchUpdateList(4,list4);
        dmsStoreService.batchUpdateList(5,list5);
    }
}
